package ar.fiuba.tp7552.buildadmin.webactions;

import java.util.HashMap;
import java.util.Map;

import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import ar.fiuba.tp7552.buildadmin.model.auth.User;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {
	
	protected Map<String, Object> params;
	protected Map<String, Object> httpParams;

	public void setParams(Map<String, Object> params) {
		this.params = params;
	}

	public void addParam(String key, Object value) {
		if (params == null) {
			params = new HashMap<String, Object>();
		}
		params.put(key, value);
	}

	public Map<String, Object> getParams() {
		return params;
	}

	public static String getUsername() {
		User user = BaseAction.getCurrentUser();
		if (user != null)
			return user.getUsername();
		return null;
	}

	public static User getCurrentUser() {
		SecurityContext ctx = SecurityContextHolder.getContext();
		Object principal = ctx.getAuthentication().getPrincipal();
		if ( principal instanceof User)
			return (User) principal;
		return null;
	}

	public Map<String, Object> getSession() {
		return ActionContext.getContext().getSession();
	}

	public ActionContext getActionContext() {
		return ActionContext.getContext();
	}

	public void setParameters(Map<String, Object> httpParams) {
		this.httpParams = httpParams;
	}

	public Map<String, Object> getHttpParams() {
		return httpParams;
	}
}
